import NeDB from "nedb";
const DBList = {
    // 用户数据库
    user: new NeDB({
      autoload: true,
      timestampData: true, //自动生成时间戳
      filename: "./db/user.db", // 指定数据库文件路径
    }),
    // 部门数据库
    department: new NeDB({
      autoload: true,
      timestampData: true, //自动生成时间戳
      filename: "./db/department.db", // 指定数据库文件路径
    }),
    // m3u8数据库
    m3u8: new NeDB({
      autoload: true,
      // timestampData: true, //自动生成时间戳
      filename: "./db/m3u8.db", // 指定数据库
    }),
    // 车辆信息数据库
    vehicleInformation: new NeDB({
      autoload: true,
      // timestampData: true, //自动生成时间戳
      filename: "./db/vehicleInformation.db", // 指定数据库
    }),
    // 车辆调度数据库
    vehicleDispatch: new NeDB({
      autoload: true,
      // timestampData: true, //自动生成时间戳
      filename: "./db/vehicleDispatch.db", // 指定数据库
    }),
    // 报表数据库
    report: new NeDB({
      autoload: true,
      timestampData: true, //自动生成时间戳
      filename: "./db/report.db", // 指定数据库文件路径
    }),
    // 报表日程数据库
    calendar: new NeDB({
      autoload: true,
      timestampData: true, //自动生成时间戳
      filename: "./db/calendar.db", // 指定数据库文件路径
    }),
  },
  DBserve = (arg) => {
    return new Promise((resolve, reject) => {
      const { DBname = "", order = "", option = {} } = arg;
      if (!DBname) {
        return reject({
          code: 0,
          message: "未获取数据库名！",
        });
      }
      const _DB = DBList[DBname];
      if (!order) {
        return reject({
          code: 0,
          message: "未获取到指令！",
        });
      }
      switch (order) {
        case "insert":
          _DB.insert(option, function (err, data) {
            if (err) {
              return reject({
                code: 0,
                message: err,
              });
            }
            return resolve({
              code: 200,
              message: "新增成功！",
              data,
            });
          });
          break;
        case "find":
          _DB
            .find(option, { password: 0 })
            .sort({ createTime: -1 })
            .exec(function (err, data) {
              if (err) {
                return reject({
                  code: 0,
                  message: err,
                });
              }
              return resolve({
                code: 200,
                message: "查询列表成功！",
                data,
              });
            });
          break;
        case "findPassword":
          _DB
            .find(option)
            .sort({ createTime: -1 })
            .exec(function (err, data) {
              if (err) {
                return reject({
                  code: 0,
                  message: err,
                });
              }
              return resolve({
                code: 200,
                message: "查询列表成功！",
                data,
              });
            });
          break;
        case "findOne":
          _DB.findOne(option, { password: 0 }, function (err, data) {
            if (err) {
              return reject({
                code: 0,
                message: err,
              });
            }
            return resolve({
              code: 200,
              message: "查询成功！",
              data,
            });
          });
          break;
        case "update":
          if (!option?._id) {
            return reject({
              code: 0,
              message: "未获取到id",
            });
          }
          _DB.update(
            { _id: option._id },
            { $set: option },
            function (err, data) {
              if (err) {
                return reject({
                  code: 0,
                  message: err,
                });
              }
              return resolve({
                code: 200,
                message: "更新成功！",
                data,
              });
            }
          );
          break;
        case "remove":
          if (!option?._id) {
            return reject({
              code: 0,
              message: "未获取到id",
            });
          }
          _DB.remove({ _id: option._id }, {}, function (err, data) {
            if (err) {
              return reject({
                code: 0,
                message: err,
              });
            }
            return resolve({
              code: 200,
              message: "删除成功！",
              data,
            });
          });
          break;
        case "count":
          _DB.count(option, function (err, count) {
            if (err) {
              return reject({
                code: 0,
                message: "error",
                data: 0,
              });
            } else {
              return resolve({
                code: 200,
                message: "success",
                data: count,
              });
            }
          });
          break;
        default:
          break;
      }
    });
  };

export default DBserve;
